package day03;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重入锁实现 先打印2再打印1   条件变量的await 和 signal
 */
@Slf4j
public class test6 {
    static ReentrantLock lock = new ReentrantLock();
    static Condition wait1 = lock.newCondition();
    // 标识
    static boolean flag = false;
    public static void main(String[] args) {
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                lock.lock();
                while (!flag) {
                    try {
                        wait1.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.debug("1");
                try{

                }finally {
                    lock.unlock();
                }
            }
        };

        Thread t2 = new Thread("t2") {
            @Override
            public void run() {
                lock.lock();
                log.debug("2");
                flag = true;
                wait1.signal();
                try{
                    // try尝试
                }finally {
                    lock.unlock();
                }
            }
        };

        t1.start();
        t2.start();
    }
}
